package com.huak.zookeeper.distributedlock.config;

import com.huak.zookeeper.distributedlock.util.DistributedLock;
import com.huak.zookeeper.distributedlock.util.ZkCurator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liuhuan
 * @date 2018/11/17 16:36
 * @description
 */
@Configuration
public class CuratorConfig {


    @Bean
    public RetryNTimes retryNTimes(){
        //重试次数，间隔时间
        RetryNTimes retryNTimes=new RetryNTimes(10,5000);
        return retryNTimes;
    }

    @Bean(initMethod = "start")
    public CuratorFramework curatorFrameworkFactory(){
        CuratorFramework client=CuratorFrameworkFactory.newClient("172.16.9.12:2181",1000,50000,retryNTimes());

        return client;
    }

    @Bean(initMethod ="init" )
    public ZkCurator zkCurator(){
        ZkCurator zkCurator=new ZkCurator(curatorFrameworkFactory());
        return zkCurator;
    }

    @Bean(initMethod = "init")
    public DistributedLock distributedLock(){
        DistributedLock lock=new DistributedLock(curatorFrameworkFactory());
        return lock;
    }
}
